package com.zxbit.flash.presenter;

import android.graphics.Bitmap;

import com.zxbit.flash.base.BasePresenter;
import com.zxbit.flash.common.UserInfo;
import com.zxbit.flash.mvpview.ImgMvpView;
import com.zxbit.flash.network.CustomConsumer;
import com.zxbit.flash.network.ParamsFactory;
import com.zxbit.flash.response.IdentityResponse;
import com.zxbit.flash.utils.ImageUtils;

import java.io.File;
import java.util.HashMap;
import java.util.Map;

import io.reactivex.functions.Consumer;
import okhttp3.FormBody;
import okhttp3.MediaType;
import okhttp3.MultipartBody;
import okhttp3.RequestBody;

public class ImgPresenter extends BasePresenter {
    private ImgMvpView mvpView;

    public ImgPresenter(ImgMvpView mvpView) {
        this.mvpView = mvpView;
    }

    public void uploadIdentityCard(File file) {
        Map<String, Object> fromMap = new HashMap<>();
        fromMap.put("userUuid", UserInfo.getSaveUuid());
        fromMap.put("fileType", 1);
        HashMap<String, Object> map = new HashMap<>();
        map.put("paramStr", ParamsFactory.getCommonParamsStr(fromMap));

        RequestBody requestBody = FormBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "file", requestBody);

        toSubScribe(req.uploadIdentityCard(UserInfo.getToken(), map, filePart), new CustomConsumer<IdentityResponse>() {
            @Override
            public void call(IdentityResponse identityResponse) {
                mvpView.onGetDataNext(identityResponse, 1);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    public void uploadImg(final int type, File file) {

        Map<String, Object> fromMap = new HashMap<>();
        fromMap.put("userUuid", UserInfo.getSaveUuid());
        fromMap.put("fileType", type);

        HashMap<String, Object> map = new HashMap<>();
        map.put("paramStr", ParamsFactory.getCommonParamsStr(fromMap));

        RequestBody requestBody = FormBody.create(MediaType.parse("multipart/form-data"), file);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "file", requestBody);

        toSubScribe(req.uploadSingleImg(UserInfo.getToken(), map, filePart), new CustomConsumer<IdentityResponse>() {
            @Override
            public void call(IdentityResponse identityResponse) {
                mvpView.onGetDataNext(identityResponse, type);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }

    /**
     * 通过bitmap上传身份证
     *
     * @param bitmap
     */
    public void uploadIdentityCardForBitmap(final int type,Bitmap bitmap) {
        Map<String, Object> fromMap = new HashMap<>();
        fromMap.put("userUuid", UserInfo.getSaveUuid());
        fromMap.put("fileType", type);
        HashMap<String, Object> map = new HashMap<>();
        map.put("paramStr", ParamsFactory.getCommonParamsStr(fromMap));
        byte[] bitmapByte = ImageUtils.bitmap2Bytes(bitmap, Bitmap.CompressFormat.JPEG);
        RequestBody requestBody = FormBody.create(MediaType.parse("multipart/form-data"),
                bitmapByte);
        MultipartBody.Part filePart = MultipartBody.Part.createFormData("file", "file",
                requestBody);

        toSubScribe(req.uploadIdentityCard(UserInfo.getToken(), map, filePart), new CustomConsumer<IdentityResponse>() {
            @Override
            public void call(IdentityResponse identityResponse) {
                mvpView.onGetDataNext(identityResponse, 1);
            }
        }, new Consumer<Throwable>() {
            @Override
            public void accept(Throwable throwable) throws Exception {
                mvpView.onGetDataError(throwable);
            }
        });
    }
}
